package com.demo;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;

public class interview {

    public static void main(String[] args) {
        int[] nums = {12,656,45,67,32,6,7,1,12};
        int i = majorityElement(nums);
        System.out.println(i);
    }

    public static int majorityElement(int[] array) {
        int[] count = new int[1000];
        for(int i = 0; i < array.length; i++) {
            count[array[i]]++;
        }
        int maxCount = count[0];
        int maxNumber = 0;
        for(int i = 1; i < 100; i++) {
            if(count[i] > maxCount)
                maxCount = count[i];
        }
        for(int i = 0; i < 100; i++) {
            if(count[i] == maxCount)
                maxNumber = i;
        }
        System.out.println("出现次数最多的数字为：" + maxNumber);
        System.out.println("该数字一共出现" + maxCount + "次");
        return maxNumber;
    }

    /*
    * 双重判定锁
    * */
    private interview(){}
    private static interview single ;
    public static interview getInstance(){
        if (single == null){
            synchronized (single){
                if (single == null){
                    single = new interview();
                }
            }
        }
        return single;
    }




    /**
     * 方法说明:
     * @param nums
     * @return int[]
     * @version v1.0
     * @author 梁阿龙
     * @date 2022-12-02 10:02
     * */
    public static int[]  bubbleSort(int[] nums){
        int length = nums.length;
        if (length == 0 || length == 1){
            return nums;
        }
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - i -1; j++) {
                if (nums[j]>nums[j+1]){
                    int temp = nums[j+1];
                    nums[j+1] = nums[j];
                    nums[j] = temp;
                }
            }
        }
        return nums;
    }
}
